package com.mlm.mlmapp.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Receiver;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.ColorRes;
import org.androidannotations.annotations.res.DimensionRes;
import org.androidannotations.annotations.res.DrawableRes;
import org.androidannotations.annotations.sharedpreferences.Pref;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView.Adapter;
import android.util.Log;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.flyco.tablayout.view.CouponMineFragment;
import com.flyco.tablayout.view.FixedSlidingTabLayout;
import com.flyco.tablayout.view.OrderFragment;
import com.mlm.mlmapp.R;
import com.mlm.mlmapp.adapter.view.listener.OnNextPageShouldDownloadListener;
import com.mlm.mlmapp.cons.HTTPCons;
import com.mlm.mlmapp.interfaces.Coupon;
import com.mlm.mlmapp.interfaces.Prefs_;
import com.mlm.mlmapp.utils.CommonUtils;

/**
 * Activity which displays a login screen to the user, offering registration as
 * well.
 */
@EActivity(R.layout.coupon_mine)
public class CouponMineActivity extends BaseActivity implements HTTPCons {
	// 状态 1、未使用 2、已使用3、已到期
	private static final int TYPE_UNUSED = 1;
	private static final int TYPE_USED = 2;
	private static final int TYPE_INVALID = 3;
	public static final String COUPON_REFRESH_FLAG="com.mlm.mlmapp.coupon.refresh";
	@Pref
	Prefs_ pref;
	@DimensionRes(R.dimen.recyclerview_mycomments_space)
	float commentImagesMargin;
	@DimensionRes
	float comments_images_edge;
	@ColorRes
	int red;
	@ColorRes
	int lightgreytext;
	@ColorRes
	int blacktext;
	private final String[] mTitles = { "未使用", "已使用", "已到期" };
	@ViewById
	TextView tv_title;
	@ViewById
	TextView tv_no_title;
	@ViewById
	FixedSlidingTabLayout tl;
	@DrawableRes
	Drawable grey_edge_btn_long;
	@ViewById
	ViewPager vp;
	@ViewById
	RadioGroup top_group;
	private ArrayList<Fragment> mFragments = new ArrayList<>();

	@AfterViews
	void init() {
		String title=getTitle().toString();
		if(!"".equals(title)){
			((TextView) this.findViewById(R.id.tv_title)).setText(title);
		}
		vp.setAdapter(new MyPagerAdapter(this.getSupportFragmentManager()));
		vp.setOffscreenPageLimit(3);
		vp.setKeepScreenOn(true);
		mFragments.add(CouponMineFragment
				.getInstance(pref.loginID().get(), TYPE_UNUSED));
		mFragments.add(CouponMineFragment.getInstance(pref.loginID().get(),
				TYPE_USED));
		mFragments.add(CouponMineFragment.getInstance(pref.loginID().get(),
				TYPE_INVALID));
		vp.getAdapter().notifyDataSetChanged();
		tl.setViewPager(vp, mTitles);
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		Intent intent = new Intent();
		this.setResult(RESULT_CANCELED, intent);
		this.finish();
	}
	@Receiver(actions =CouponMineActivity.COUPON_REFRESH_FLAG, registerAt = Receiver.RegisterAt.OnCreateOnDestroy)
	protected void onCouponRefresh(Intent intent) {
		if (intent.getStringExtra("refreshflag")!=null&&"0".equals(intent.getStringExtra("refreshflag"))){
			for (Fragment f : mFragments) {
				((CouponMineFragment) f).refresh();
			}
		}


	}
	@Click
	void btn_back() {
		onBackPressed();
	}

	private class MyPagerAdapter extends FragmentPagerAdapter {
		public MyPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public int getCount() {
			return mFragments.size();
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return "";
		}

		@Override
		public Fragment getItem(int position) {
			return mFragments.get(position);
		}
	}



}
